package cloud.study.template.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author : forget.liu@aliyun.com
 * @date : 2020/3/3 15:20
 * @description : TODO
 */
@Configuration
public class WebConfigurer implements WebMvcConfigurer {


    private String[] excludePathPatterns = new String[]{"/swagger-resources/**",
            "/webjars/**", "/v2/**", "/swagger-ui.html/**",
            "/error/**"};


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    }

    /**
     * 配置接口文档
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }


    /**
     * 配置跨域
     *
     * @param
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedMethods(HttpMethod.GET.name(),
                        HttpMethod.HEAD.name(),
                        HttpMethod.POST.name(),
                        HttpMethod.PUT.name(),
                        HttpMethod.PATCH.name(),
                        HttpMethod.DELETE.name(),
                        HttpMethod.OPTIONS.name(),
                        HttpMethod.TRACE.name())
                .allowCredentials(true)
                .maxAge(3600);
    }



}
